package com.frxuexi.shop2307.fragment

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.frxuexi.lib_base.mvvm.BaseFragment
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.shop2307.R
import com.frxuexi.shop2307.adapter.ShopAdapter
import com.frxuexi.shop2307.adapter.TypeOneAdapter
import com.frxuexi.shop2307.adapter.TypeTwoAdapter
import com.frxuexi.shop2307.databinding.FragmentTypeBinding
import com.frxuexi.shop2307.shop.ShopViewModel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import dagger.hilt.android.AndroidEntryPoint


@AndroidEntryPoint
class TypeFragment : BaseFragment<FragmentTypeBinding,ShopViewModel>() {
    val typeOneAdapter by lazy { TypeOneAdapter() }
    val typeTwoAdapter by lazy { TypeTwoAdapter() }
    val shopAdapter by lazy { ShopAdapter() }

    var page=1
    var total=0
    var pid=0
    override fun initData() {
        vm.productTwoTypeData.observe(this){
            typeOneAdapter.apply {
                data.clear()
                it.get(0).isCheck=true
                addData(it)
                notifyDataSetChanged()

                typeTwoAdapter.apply {
                    data.clear()
                    addData(typeOneAdapter.data.get(0).child)
                    notifyDataSetChanged()
                }
            }
            pid=it.get(0).pid
            vm.getShopByKey(pid,"",10,page)
        }

        vm.shopKeyData.observe(this){
            total=it.totalPage
            shopAdapter.apply {
                data.clear()
                addData(it.list)
                notifyDataSetChanged()
            }
        }
    }



    override fun initView() {

        binding.typeRv.apply {
            adapter=typeOneAdapter
            layoutManager=LinearLayoutManager(requireContext())
        }

        typeOneAdapter.setOnItemClickListener { adapter, view, position ->
            typeOneAdapter.data.forEach { it.isCheck=false }
            typeOneAdapter.data.get(position).isCheck=true
            typeOneAdapter.notifyDataSetChanged()

            typeTwoAdapter.apply {
                data.clear()
                addData(typeOneAdapter.data.get(position).child)
                notifyDataSetChanged()
            }
        }

        binding.typeRvTwo.apply {
            adapter=typeTwoAdapter
            layoutManager=GridLayoutManager(requireContext(),3)
        }

        binding.typeRvThree.apply {
            adapter=shopAdapter
            layoutManager=GridLayoutManager(requireContext(),2)
        }

        vm.getProductsByTwoType()

        typeTwoAdapter.setOnItemClickListener { adapter, view, position ->
            pid=typeTwoAdapter.data.get(position).pid
            page=1
            vm.getShopByKey(pid,"",10,page)
        }

        shopAdapter.setOnItemClickListener { adapter, view, position ->
            ArouterManager.nav(requireContext(),"/app/ShopDetailActivity",shopAdapter.data.get(position).id)
        }
        binding.typeSmart.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                page=1
                vm.getShopByKey(pid,"",10,page)
                binding.typeSmart.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if(page<total) {
                    page++
                    vm.getShopByKey(pid,"",10,page)
                }
                binding.typeSmart.finishLoadMore()
            }
        })

        binding.typeEt.addTextChangedListener(object :TextWatcher{
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                page=1
                vm.getShopByKey(pid,s.toString(),10,page)
            }

            override fun afterTextChanged(s: Editable?) {
            }
        })
    }

}